<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inicio extends CI_Controller {
	
	function __construct() {
		parent::__construct();
		
	}
	public function index(){
		$session_data = $this->session->userdata('grambel');
		$data["session_data"] = $session_data;
		
		$setor = new Setor();
		$setor->where('tag <>',"")->group_by(' RAND()')->limit('3')->get();
		
		//echo $setor->check_last_query();
		
		$data["tag"] = $setor;
		$this->load->view('header');
		$this->load->view('inicio', $data);
	}
	
	public function salvar(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			$ate = new Atendimento();
			
			$ate->usuario_id = $session_data["usuario_id"];
			$ate->ramal = "(43) 3902-".$this->input->post("ramal");
			$ate->solicitante = strtoupper ($this->input->post("solicitante"));
			$ate->telefone = $this->input->post("telefone");
			$ate->solicitado = strtoupper ($this->input->post("solicitado"));
			$ate->assunto = strtoupper ($this->input->post("assunto"));
			$ate->tipoligacao = $this->input->post("tipoligacao");
			$ate->dt_status = date('Y-m-d H:i:s');
			
			$ate->save();
			
			$acao = $ate->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->save();
			
		}else{
			echo 'ERROR - EMPTY';
		}
	}
	
	public function editar(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		
		if($this->input->post()){
		
			$ate = new Atendimento();
			$ate = $ate->where('id',$this->input->post('id'))->get();
			
			
			$ate->ramal = $this->input->post("ramal1");
			$ate->solicitante = strtoupper ($this->input->post("solicitante1"));
			$ate->telefone = $this->input->post("telefone1");
			$ate->solicitado = strtoupper ($this->input->post("solicitado1"));
			$ate->assunto = strtoupper ($this->input->post("assunto1"));
			$ate->tipoligacao = $this->input->post("tipoligacao1");
			
			$ate->save();
			
			$acao = $ate->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->save();
			
		}else{
			echo 'ERROR - EMPTY';
		}
		
	}
	
	public function atendimento(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$atendimento = new Atendimento();
		$atendimento = $atendimento->consultaAtendimento($session_data);
		$data["atendimentos"] = $atendimento;
		
		$this->load->view('header');
		$this->load->view('view_atendimento', $data);
	}
	
	public function atendimentoModal($id){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		$atendimento = new Atendimento();
		$atendimento = $atendimento->where('id',$id)->get();
		$data["dados"] = $atendimento;
		//$this->load->view('header', $data);
		$this->load->view('view_atendimento_modal', $data);
	}
	
	public function consulta(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$atendimento = new Atendimento();
		$atendimento = $atendimento->consulta();
		$data["atendimentos"] = $atendimento;
		
		$this->load->view('header');
		$this->load->view('view_atendimento_consulta', $data);
	}
	public function meusatendimentos(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$atendimento = new Atendimento();
		$atendimento = $atendimento->consultaMeusAtendimentos($session_data);
		$data["atendimentos"] = $atendimento;
		
		$this->load->view('header');
		$this->load->view('view_atendimento_meus', $data);
	}
	public function consultaSolicitacoes(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		$soli = new Solicitacao();
		$soli->where("status","1")->get();
		$data["solicitacao"] = $soli;
		
		$this->load->view('header');
		$this->load->view('view_solicitacao_consulta', $data);
	}
	public function relatorioatendimento(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post()){
		
			//------------------------------------------------------------------------------------------------------------------------
		
			$pdf = new Pdf(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
			gerarRelatorio($pdf);
			$pdf->AddPage('L', 'A4');
			
			// TITULO DA PAGINA
			$pdf->Write(0, 'CONTROLE DE TELEFONEMAS', '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			// TITULO DA PAGINA
			$pdf->Write(0, 'Periodo: '.$this->input->post('inicio').' a '.$this->input->post('fim'), '', 0, 'C', true, 0, false, false, 0);
			$pdf->Ln();
			
			$ate = new Atendimento();
			$ate = $ate->relatorioAtendimento($this->input->post('tipoligacao'),brToUsa($this->input->post('inicio')),brToUsa($this->input->post('fim')),$this->input->post('usuario'));
			

			//*************************************************************************************
			// CABEÇALHO
			//*************************************************************************************
			$header = array('Data','Ramal','Solicitante','Telefone','Solicitado','Assunto','DDD','Responsavel');
			// Colors, line width and bold font
			$pdf->SetFillColor(208, 208, 208);
			$pdf->SetTextColor(0);
			$pdf->SetDrawColor(0, 0, 0);
			$pdf->SetLineWidth(0.3);
			$pdf->SetFont('times', 'B', 7);
			
			// Header
			$w = array(23, 20, 40,20, 40, 50,20, 50);
			$num_headers = count($header);
			for($i = 0; $i < $num_headers; ++$i) {
				$pdf->Cell($w[$i], 7, $header[$i], 1, 0, 'C', 1);
			}
			$pdf->Ln();
			//*************************************************************************************
			// DADOS
			//*************************************************************************************
			// Color and font restoration
			$pdf->SetFillColor(224, 235, 255);
			$pdf->SetTextColor(0);
			$pdf->SetFont('times', '', 7);
			// Data
			$fill = 0;

			foreach($ate as $r){	
			
				$pdf->Cell($w[0], 6,$r->dt_cadastro, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[1], 6,$r->ramal, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[2], 6,$r->solicitante, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[3], 6,$r->telefone, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[4], 6,$r->solicitado, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[5], 6,$r->assunto, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[6], 6,$r->tipoligacao, 'LR', 0, 'L', $fill);
				$pdf->Cell($w[7], 6,$r->nome_completo, 'LR', 0, 'L', $fill);
				$pdf->Ln();
				$fill=!$fill;
				
			}
			// FINALIZA TABELA
				$pdf->Cell(array_sum($w), 0, '', 'T');
			
				
		
		/*
		$html = "";
		$pdf->writeHTML($html, true, false, true, false, '');
		*/
		
		$pdf->Output('Relatorio_Devolucao.pdf', 'I');
			
			
		}else{
				
			
			$usuario = new Usuario();
			$usuario->where("permissao_id","2")->get();
			$aux = array();
			$aux["999"] = " TODAS ";
			foreach($usuario as $s){
				$aux[$s->id ] = $s->nome_completo;
			}
		
			$data["usuario"] = $aux;
			
			$this->load->view('header');
			$this->load->view('view_atendimento_relatorio', $data);
		}
	}
	public function sobre(){
		$this->load->view('header');
		$this->load->view('view_sobre');
	}
	public function sorry(){
		$this->load->view('header');
		$this->load->view('view_sorry');
	}
	public function aguardar(){
		if($this->input->post("id")){
			$ate = new Atendimento();
			$ate->where("id", $this->input->post("id"))->get();
			$ate->tentativas = $ate->tentativas + 1;
			$ate->dt_status = date('Y-m-d H:i:s');
			$ate->save();
			
			$acao = $ate->check_last_query(false,true);
			$log = new Log();
			$log->ip = $this->session->userdata('ip_address');
			$log->usuario = $session_data["nome"];
			$log->acao = $acao;
			$log->save();
		}
	}
	public function finalizar(){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if($this->input->post("id")){
			$msg = '';
			foreach($this->input->post("id") as $t){
			
				$ate = new Atendimento();
				$ate->where_in("id",$t )->get();
				$ate->status = $this->input->post("status");
				
				if($ate->save()){
					$acao = $ate->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
					$msg .= "ATENDIMENTO FINALIZADO\n";
				}
			}			
			echo $msg;
		}else{
			echo 'SELECIONE UM ATENDIMENTO';
		}
	}
	public function ligardenovo($id){
		//******************************************************
		//   PERMISSAO 
		//******************************************************
		$session_data = $this->session->userdata('grambel');
		temPermissao(array('1','2'),$session_data);
		
		if(!empty($id)){
			$ate = new Atendimento();
			$ate->where("id", $id)->get();
			
			if($ate->exists()){
				$novo = new Atendimento();
				$valida = new Atendimento();
				
				//CASO TENHA UM ATENDIMENTO ABERTO DESTE NUMERO, NAO ABRE OUTRO ATENDIMENTO
				$valida = $valida->emAtendimento($ate->ramal);
				
				if($valida->exists()){
					/*
					echo "<script type='text/javascript'>";
					echo "alert('TELEFONE EM ATENDIMENTO');";
					echo "window.load('".base_url('index.php/inicio/atendimento')."')";
					echo "</script>";*/
					echo "TELEFONE EM ATENDIMENTO";
					
				}else{
					$novo->usuario_id = $session_data["usuario_id"];
					$novo->ramal = $ate->ramal;
					$novo->solicitante = $ate->solicitante;
					$novo->telefone = $ate->telefone;
					$novo->solicitado = $ate->solicitado;
					$novo->assunto = $ate->assunto;
					$novo->tipoligacao = $ate->tipoligacao;

					$novo->save();
					
					$acao = $novo->check_last_query(false,true);
					$log = new Log();
					$log->ip = $this->session->userdata('ip_address');
					$log->usuario = $session_data["nome"];
					$log->acao = $acao;
					$log->save();
			
					echo "ATENDIMENTO REABERTO";
				}
			}
		}
	}
	public function validaTelefone(){
	
		$atendimento = new Atendimento();
		$atendimento = $atendimento->telefoneEmAtendimento($this->input->get("telefone"));
		if($atendimento->exists()){
			echo "false";
		}else{ 
			echo "true";
		}
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */